package com.ekuaitu.kuaitustaff.utils.ble.bean;

import com.ekuaitu.kuaitustaff.utils.ble.utils.ConvertHelper;

/**
 * Created by xiongjida on 2017/7/7.
 */

public class ZisCarStatusBean extends ZisBaseBean {

    public int CmdID;
    public String LightState;
    public String DoorState;
    public String OtherState;
//    public String OD_EngineState;
//    public String OD_LockState;
//    public String OD_DoorState;


    /**
     * 车辆状态
     *
     * @param idc
     * @param receivedTime
     * @param bytes
     * @param start
     * @return 7e10 001c 005d 0a07 0000595f3d90030102000002020000030500000000008fe2
     * 00a0 0a07 0000595f5c2f03010200000202057003054500440031
     */
    public static ZisCarStatusBean CreateBean(String idc, long receivedTime, byte[] bytes, int start) {
        ZisCarStatusBean bean = new ZisCarStatusBean();
        bean.IDC = idc;
        bean.ReceivedTime = receivedTime;

        int n = start + 2;

        bean.CmdID = ConvertHelper.bytesToInt(bytes[n], bytes[n + 1], bytes[n + 2], bytes[n + 3]);
        n += 4;

        int len = bytes[n++];
        if (len < 3)
            throw new RuntimeException("[ZisCarStatusBean]length of items is less than 3.");

        for (int i = 0; i < len; i++) {
            switch (bytes[n++]) {
                case 1:
                    if (bytes[n] < 2)
                        throw new RuntimeException("[ZisCarStatusBean]length of light state is less than 2.");
                    bean.LightState = ConvertHelper.byteArrayToHexString(bytes, n + 1, bytes[n]);

                    break;
                case 2:
                    if (bytes[n] < 2)
                        throw new RuntimeException("[ZisCarStatusBean]length of door state is less than 2.");
                    bean.DoorState = ConvertHelper.byteArrayToHexString(bytes, n + 1, bytes[n]);
                    break;
                case 3:
                    if (bytes[n] < 3)
                        throw new RuntimeException("[ZisCarStatusBean]length of other state is less than 3.");
                    bean.OtherState = ConvertHelper.byteArrayToHexString(bytes, n + 1, bytes[n]);
                    break;
//                case (byte) 0xa1:
//                    if (bytes[n] < 1)
//                        throw new RuntimeException("[ZisCarStatusBean]length of 0xa1 state is less than 1.");
//                    bean.OD_EngineState = ConvertHelper.byteArrayToHexString(bytes, n + 1, bytes[n]);
//                    break;
//                case (byte) 0xa2:
//                    if (bytes[n] < 1)
//                        throw new RuntimeException("[ZisCarStatusBean]length of 0xa2 state is less than 1.");
//                    bean.OD_DoorState = ConvertHelper.byteArrayToHexString(bytes, n + 1, bytes[n]);
//                    break;
//                case (byte) 0xa3:
//                    if (bytes[n] < 1)
//                        throw new RuntimeException("[ZisCarStatusBean]length of 0xa3 state is less than 1.");
//                    bean.OD_LockState = ConvertHelper.byteArrayToHexString(bytes, n + 1, bytes[n]);
//                break;
            }
            n += 1 + bytes[n];
        }

        return bean;
    }


    public static CarRequireStatus AnalyticStatusNew(String LightState, String DoorState, String OtherState) {
        //   models.CarRequireStatus cr = new models.CarRequireStatus();
        CarRequireStatus cr = new CarRequireStatus();

//        int markerLamp = -1;//示廓灯
        int dippedHeadlight = -1;//近光灯
//        int highBeam = -1;//远光灯
//        int foglight = -1;//雾灯
//        int IndicatorLeft = -1;//左转向灯
//        int IndicatorRight = -1;//右转向灯
//        int hazardWarningLamp = -1;//危险报警灯
//        int troubleLamp = -1;//故障报警灯

        int ZV = -1;//中控锁
        int rearRight = -1;//右后门
        int rearLeft = -1;//左后门
        int frontRight = -1;//右前门
        int frontLeft = -1;//左前门

        int boot = -1;//后备箱
//        int seatBelt = -1;//安全带
//        int ElectricalWIPERFr = -1;//前雨刷
//        int parkBrake = -1;//驻车制动器
//        int brakeLights = -1;//刹车踏板
//        int Airconditioning = -1;//空调
//        int gears = -1;//档位
        int engine = -1;//发动机状态
//        int LF_Window = -1;//左前窗
//        int LR_Window = -1;//左后窗
//        int RR_Window = -1;//右后窗
//        int FR_Window = -1;//右前窗

//        int SunRoof = -1;//天窗
//        int ACC = -1;//ACC
        int cdzt = -1;//充电状态

        int on = -1;
        try {
            if (LightState.length() <= 0) LightState = "0000";
            if (DoorState.length() <= 0) DoorState = "0000";
            if (OtherState.length() <= 0) OtherState = "0000000000";


            byte[] lights = ConvertHelper.hexStringToByteArray(LightState);
            byte[] doors = ConvertHelper.hexStringToByteArray(DoorState);
            byte[] others = ConvertHelper.hexStringToByteArray(OtherState);

//            foglight = (lights[0] & 0x1);//是否支持雾灯
//            int foglightValue = (((lights[0] & 0x2) >> 1));//是否支持雾灯
//
//            highBeam = (((lights[0] & 0x4) >> 2));//是否支持远光灯
//            int highBeamValue = (((lights[0] & 0x8) >> 3));//远光灯
//
            dippedHeadlight = (((lights[0] & 0x10) >> 4));//是否支持近光灯
            int dippedHeadlightValue = (((lights[0] & 0x20) >> 5));//近光灯
//
            //如果近远灯都为开时，只打开近光
//            if (dippedHeadlightValue == 1 && highBeamValue == 1) {
//                highBeamValue = -1;
//            }
//
//            markerLamp = (((lights[0] & 0x40) >> 6));//是否支持示廓灯
//            int markerLampValue = (((lights[0] & 0x80) >> 7));//示廓灯
//
//            troubleLamp = (lights[1] & 0x1);//是否支持故障报警灯
//            int troubleLampValue = (((lights[1] & 0x2) >> 1));//故障报警灯
//
//            hazardWarningLamp = (((lights[1] & 0x4) >> 2));//是否支持危险报警灯
//            int hazardWarningLampValue = (((lights[1] & 0x8) >> 3));//危险报警灯
//
//            IndicatorLeft = (((lights[1] & 0x10) >> 4));//是否支持左转向灯
//            int IndicatorLeftValue = (((lights[1] & 0x20) >> 5));//左转向灯
//
//            IndicatorRight = (((lights[1] & 0x40) >> 6));//是否支持右转向灯
//            int IndicatorRightValue = (((lights[1] & 0x80) >> 7));//右转向灯
//            if (foglight == 1)
//                cr.foglight = foglightValue;
//            if (highBeam == 1)
//                cr.highBeam = highBeamValue;
            if (dippedHeadlight == 1)
                cr.dippedHeadlight = dippedHeadlightValue;
//            if (markerLamp == 1)
//                cr.markerLamp = markerLampValue;
//            if (troubleLamp == 1)
//                cr.troubleLamp = troubleLampValue;
//            if (hazardWarningLamp == 1)
//                cr.hazardWarningLamp = hazardWarningLampValue;
//            if (IndicatorLeft == 1)
//                cr.IndicatorLeft = IndicatorLeftValue;
//            if (IndicatorRight == 1)
//                cr.IndicatorRight = IndicatorRightValue;

            frontLeft = (doors[0] & 0x1);//左前门
            int frontLeftValue = (((doors[0] & 0x2) >> 1));
            frontRight = (((doors[0] & 0x4) >> 2));//右前门
            int frontRightValue = (((doors[0] & 0x8) >> 3));
            rearLeft = (((doors[0] & 0x10) >> 4));//左后门
            int rearLeftValue = (((doors[0] & 0x20) >> 5));
            rearRight = (((doors[0] & 0x40) >> 6));//右后门
            int rearRightValue = (((doors[0] & 0x80) >> 7));
            if (frontLeft == 1)
                cr.frontLeft = frontLeftValue;
            if (frontRight == 1)
                cr.frontRight = frontRightValue;
            if (rearLeft == 1)
                cr.rearLeft = rearLeftValue;
            if (rearRight == 1)
                cr.rearRight = rearRightValue;


            boot = (((doors[1] & 0x40) >> 6));//后备箱
            int bootValue = (((doors[1] & 0x80) >> 7));
            if (boot == 1)
                cr.boot = bootValue;

            engine = (((doors[1] & 0x10) >> 4));//发动机
            int engineValue = (((doors[1] & 0x20) >> 5));
            if (engine == 0)
                cr.engine = -1;
            else
                cr.engine = engineValue;


            on = (doors[1] & 0x1);//ON
            int onValue = (((doors[1] & 0x2) >> 1));

            if (on == 1) {
                cr.on = onValue;
            }

//            ACC = (((doors[1] & 0x4) >> 2));//ACC
//            int ACCValue = (((doors[1] & 0x8) >> 3));
//            if (ACC == 0)
//                cr.ACC = -1;
//            else
//                cr.ACC = ACCValue;
//
//            gears = (others[2] & 0xff); //档位
//            if (gears == 0)
//                cr.gears = -1;
//            else
//                cr.gears = gears;//P	R	N	D D/R  P/N:  0x50:P	0x52:R	0x4E:N	0x44:D  0x42:D/R  0x40:P/N
//
//
//            Airconditioning = (((others[1] & 0x40) >> 6));//空调
//            int othersValue = (((others[1] & 0x80) >> 7));
//            if (Airconditioning == 1)
//                cr.Airconditioning = othersValue;

//            brakeLights = (others[0] & 0x1);//刹车踏板
//            int brakeLightsValue = (((others[0] & 0x2) >> 1));
//            parkBrake = (((others[0] & 0x4) >> 2));//驻车制动器
//            int parkBrakeValue = (((others[0] & 0x8) >> 3));
//            ElectricalWIPERFr = (((others[0] & 0x10) >> 4));//前雨刷
//            int ElectricalWIPERFrValue = (((others[0] & 0x20) >> 5));
//            seatBelt = (((others[0] & 0x40) >> 6));//安全带
//            int seatBeltValue = (((others[0] & 0x80) >> 7));
//            if (brakeLights == 1)
//                cr.brakeLights = brakeLightsValue;
//            if (parkBrake == 1)
//                cr.parkBrake = parkBrakeValue;
//            if (ElectricalWIPERFr == 1)
//                cr.ElectricalWIPERFr = ElectricalWIPERFrValue;
//            if (seatBelt == 1)
//                cr.seatBelt = seatBeltValue;

//            LF_Window = (others[3] & 0x1);//左前窗
//            int LF_WindowValue = (((others[3] & 0x2) >> 1));
//            FR_Window = (((others[3] & 0x4) >> 2));//右前窗
//            int FR_WindowValue = (((others[3] & 0x8) >> 3));
//            LR_Window = (((others[3] & 0x10) >> 4));//左后窗
//            int LR_WindowValue = (((others[3] & 0x20) >> 5));
//            RR_Window = (((others[3] & 0x40) >> 6));//右后窗
//            int RR_WindowValue = (((others[3] & 0x80) >> 7));
//            if (LF_Window == 1)
//                cr.LF_Window = LF_WindowValue;
//            if (FR_Window == 1)
//                cr.FR_Window = FR_WindowValue;
//            if (LR_Window == 1)
//                cr.LR_Window = LR_WindowValue;
//            if (RR_Window == 1)
//                cr.RR_Window = RR_WindowValue;

            ZV = (((others[4] & 0x10) >> 4));//中控锁
            int ZVValue = ((others[4] & 0x20) >> 5);
            //  ZV = (((others[4] & 0x4) >> 2) | ((OD_others[4] & 0x4) >> 2));//设防撤防
            //  int ZVValue = (((others[4] & 0x8) >> 3) | ((OD_others[4] & 0x8) >> 3));

//
//            SunRoof = (((others[4] & 0x40) >> 6));//天窗
//            int SunRoofValue = (((others[4] & 0x80) >> 7));

            if (ZV == 1)
                cr.ZV = ZVValue;

//            if (SunRoof == 1)
//                cr.SunRoof = SunRoofValue;

            cdzt = others[4] & 0x1;//充电
            int cdztValue = (others[4] & 0x2) >> 1;
            if (cdzt == 1)
                cr.chargeStatus = cdztValue;

            return cr;
        } catch (Exception ex) {
//            LogManager.WriteLog(LogFile.Trace, "车辆状态解析出错");
            return null;
        }

    }


    @Override
    public String toString() {
        return "ZisCarStatusBean{" +
                "CmdID=" + CmdID +
                ", LightState='" + LightState + '\'' +
                ", DoorState='" + DoorState + '\'' +
                ", OtherState='" + OtherState + '\'' +
                '}';
    }
}
